import shop from "@/api/shop";
import store from "../store/index";
import utils from "@/common/utils";
// #ifdef H5
import wechat from "@/common/wechat.js";
// #endif

function setShareParams(obj) {
  let store_id = uni.getStorageSync("store_id");

  if (parseInt(store_id) && obj.path.indexOf("/goods/goods/goods") != -1) {
    if (obj.path.indexOf("scene") != -1) {
    } else {
      obj.path += `&store_id=${store_id}`;
    }
  }

  return obj;
}

export default {
  install(Vue) {
    Vue.mixin({
      data() {
        return {
          storeName: uni.getStorageSync("storeName"),
          storeLogo: uni.getStorageSync("storeLogo")
        };
      },
      methods: {
        onShareEnvt() {
          try {
            this.$allrequest.coupon.recordCouponReceive((res) => {
              if (res.code == 0) {
                if (res.data) {
                  this.$store.state.gettedCoupon = res.data;
                  this.$store.state.showGetCoupon = true;
                }
              }
            });
          } catch (err) {}
        }
      },
      onLoad() {
        uni.hideTabBar();
      },
      onShow: () => {
        const routes = getCurrentPages();
        if (routes.length > 0) {
          let route = routes[0].route;
          let param = routes[0].options;
          if (routes.length > 1) {
            route = routes[routes.length - 1].route;
            param = routes[routes.length - 1].options;
          }
          if (param && param.user_id) {
            delete param.user_id;
          }
          const params = uni.$u.queryParams(param);
          let path = "/" + route + params;
          const userInfo = uni.getStorageSync("userInfo");
          if (userInfo) {
            if (path.indexOf("?") === -1) {
              path = path + "?user_id=" + userInfo.id;
            } else {
              path = path + "&user_id=" + userInfo.id;
            }
          }
          const store_id = uni.getStorageSync("store_id") || 0;
          if (path.indexOf("?") == -1) {
            path += `?store_id=${store_id}`;
          } else {
            path += `&store_id=${store_id}`;
          }

          try {
            uni.setStorageSync("share_obj", {
              title:
                store.getters.stores.store_share_title || this.storeName || "",
              path,
              imageUrl: store.getters.stores.store_share_logo,
              desc: store.getters.stores.store_share_desc
            });
          } catch (error) {
            uni.setStorageSync("share_obj", {
              title: this.storeName || "",
              path,
              imageUrl: ""
            });
          }
        }

        shop
          .getInfo({
            store_id: uni.getStorageSync("store_id")
          })
          .then((res) => {
            if (!res.code && res.data.name) {
              this.storeName = res.data.name;
              uni.setStorageSync("storeName", res.data.name);
              this.storeLogo = res.data.logo;
              uni.setStorageSync("storeLogo", res.data.logo);
            }
          });

        // #ifdef H5
        if (utils.is_webpage()) {
          wechat.initJssdk();
        }
        // #endif
      },
      onShareAppMessage() {
        this.onShareEnvt();
        let shareObj = uni.getStorageSync("share_obj");
        shareObj = setShareParams(shareObj);

        if (!shareObj) {
          return {
            title: this.storeName || "", // 默认为小程序名称
            path: "", // 默认为当前页面路径
            imageUrl: "" // 默认为当前页面的截图
          };
        }
        return shareObj;
      },
      // #ifdef MP-WEIXIN
      onShareTimeline() {
        try {
          this.$allrequest.coupon.recordCouponReceive((res) => {});
        } catch (err) {}
        let shareObj = uni.getStorageSync("share_obj");
        shareObj = setShareParams(shareObj);
        if (!shareObj) {
          return {
            title: this.storeName || "", // 默认为小程序名称
            path: "", // 默认为当前页面路径
            imageUrl: "" // 默认为当前页面的截图
          };
        }
        return shareObj;
      },
      // #endif
      computed: {
        hasOneStoreHideApply() {
          return this.$store.getters.one_store_hide_apply;
        },
        hasSingleStore() {
          return this.$store.getters.has_is_one_store;
        },
        asyncImgs() {
          return this.$store.getters.images;
        },
        controls() {
          return this.$store.getters.controls;
        },
        stores() {
          return this.$store.getters.stores;
        },
        statusBarHeight() {
          let num = uni.getSystemInfoSync().statusBarHeight || 0;
          // #ifdef MP-TOUTIAO
          num = 0;
          // #endif
          return num;
        },
        // 门店信息
        storeMdData() {
          var data = uni.getStorageSync("storeMdData");
          return data ? JSON.parse(data) : false;
        },
        // 是否展示领取优惠券的弹窗
        showGetCoupon() {
          return this.$store.getters.showGetCoupon;
        },
        // 领取的优惠券信息
        gettedCoupon() {
          return this.$store.getters.gettedCoupon;
        },
        currentPage() {
          try {
            let routes = getCurrentPages();
            return "/" + routes[routes.length - 1].route;
          } catch (error) {
            return "pages/home/home";
          }
        },
        shenheStatus() {
          return this.$store.getters["shenheStatus"];
        },
        shenheAliStatus() {
          return this.$store.getters["shenheAliStatus"];
        },
        hasHideApply() {
          return this.$store.getters.one_store_hide_apply;
        },
        isAlipy() {
          // #ifdef MP-ALIPAY
          return true;
          // #endif
          return false;
        }
      }
    });
  }
};
